"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MyPromise = void 0;
var statusObj = {
    PENDING: 'pending',
    FULFILLED: 'fulfilled',
    REJECTED: 'rejected'
};
var MyPromise = /** @class */ (function () {
    function MyPromise(executor) {
        var _this = this;
        // Promise 状态存储
        this.status = statusObj.PENDING;
        // 成功回调
        this.successCallback = [];
        // 失败回调
        this.failCallback = [];
        this.resolve = function (value) {
            // 如果状态不是等待 阻止程序向下执行
            if (_this.status !== statusObj.PENDING) {
                return;
            }
            // 将状态更改为成功
            _this.status = statusObj.FULFILLED;
            // 保存成功之后的值
            _this.value = value;
            // 判断成功回调是否存在， 如果存在，执行
            // this.successCallback && this.successCallback(this.value);
            while (_this.successCallback.length) {
                _this.successCallback.shift()();
            }
        };
        this.reject = function (reason) {
            // 如果状态不是等待 阻止程序向下执行
            if (_this.status !== statusObj.PENDING) {
                return;
            }
            // 将状态更改为失败
            _this.status = statusObj.REJECTED;
            // 保存失败后的原因
            _this.reason = reason;
            // this.failCallback && this.failCallback(this.reason);
            while (_this.failCallback.length) {
                _this.failCallback.shift()();
            }
        };
        this.then = function (successCallback, failCallback) {
            successCallback = successCallback ? successCallback : function (value) { return value; };
            failCallback = failCallback ? failCallback : function (reason) { throw reason; };
            var substitute = new MyPromise(function (resolve, reject) {
                var exception = function (flag) {
                    setTimeout(function () {
                        try {
                            var result = flag ? successCallback(_this.value) : failCallback(_this.reason);
                            // 判断 x 的值是普通值还是promise对象
                            // 如果是普通值 直接用resolve
                            // 如果是promise对象 查看promise对象返回的结果
                            // 再根据promise对象返回的结果 决定调用resolve 还是调用 reject
                            _this.resolvePromise(substitute, result, resolve, reject);
                        }
                        catch (error) {
                            reject(error);
                        }
                    }, 0);
                };
                // 判断状态
                if (_this.status === statusObj.FULFILLED) {
                    exception(true);
                }
                else if (_this.status === statusObj.REJECTED) {
                    exception(false);
                }
                else {
                    // 等待处理
                    // 存储成功回调和失败回调函数
                    _this.successCallback.push(function () {
                        exception(true);
                    });
                    _this.failCallback.push(function () {
                        exception(false);
                    });
                }
            });
            return substitute;
        };
        this.resolvePromise = function (substitute, result, resolve, reject) {
            if (substitute === result) {
                return reject(new TypeError('Chaining cycle detected for promise #<Promse>'));
            }
            if (result instanceof MyPromise) {
                // promise 对象
                result.then(resolve, reject);
            }
            else {
                // 普通值
                resolve(result);
            }
        };
        try {
            executor(this.resolve, this.reject);
        }
        catch (e) {
            this.reject(e);
        }
    }
    MyPromise.prototype.finally = function (callback) {
        return this.then(function (value) {
            return MyPromise.resolve(callback()).then(function () { return value; });
        }, function (reason) {
            return MyPromise.resolve(callback()).then(function () { throw reason; });
        });
    };
    MyPromise.prototype.catch = function (callback) {
        return this.then(function () { }, callback);
    };
    MyPromise.all = function (array) {
        var result = [];
        var count = 0;
        return new MyPromise(function (resolve, reject) {
            var addData = function (key, value) {
                result[key] = value;
                count++;
                if (count === array.length) {
                    resolve(result);
                }
            };
            var _loop_1 = function (i) {
                var current = array[i];
                if (current instanceof MyPromise) {
                    // promise 对象
                    current.then(function (value) { return addData(i, value); }, function (reason) { return reject(reason); });
                }
                else {
                    // 普通值
                    addData(i, array[i]);
                }
            };
            for (var i = 0; i < array.length; i++) {
                _loop_1(i);
            }
        });
    };
    /**
     * resolve 实现
     * @param value
     */
    MyPromise.resolve = function (value) {
        if (value instanceof MyPromise)
            return value;
        return new MyPromise(function (resolve) { return resolve(value); });
    };
    return MyPromise;
}());
exports.MyPromise = MyPromise;
